/**
 * Created by yangh on 2016/09/03.
 */
var main = {};
var myScroll;
var canLoad=true;
(function ($) {
    main.init = function () {
    	 document.addEventListener('touchmove', function (e) {
             e.preventDefault();
         }, false);

    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
    	if(!canLoad){
    		return;
    	}
    	
        if (this.y <= main.max) {
        	canLoad=false;
        	$("#tip").text("正在加载中...");
            $("#loading").show();
            var marketId=$("#marketId").val();
            var categoryId=$("#categoryId").val();
            var maxId=$("#goodsShow li:last").val();
             
            $.ajax({
        		type:"post",
	        	url:path+"/commodity/indexShowMore/"+categoryId+"?maxId="+maxId+"&marketId="+marketId,
        		success:function(resultjson){	
        			if('000' == resultjson.code){
        				var html = "";
        				for (var i = 0; i < resultjson.result.length; i++) {
        					
        				 html += '<li value="'+resultjson.result[i].goods.id+'">';
       					 html += '<a href='+path+'/commodity/goodsinfo?goodsId='+resultjson.result[i].goods.id+'>';
       					 html += '<div class="goods-pic">';
       					 html += '<img src="'+resultjson.result[i].goods.picurl+'"></div>';
       					 html += '<h3>'+resultjson.result[i].goods.name+'</h3>';
       					 html += '<div class="score">';
       					 if(resultjson.result[i].score==0){
							 	html += '<span class="scoreCon" data-score="10"> </span>';
							 }else{
								html += '<span class="scoreCon" data-score="'+resultjson.result[i].score+'"> </span>';
							 }
       					 html += ' </div>';
       					 if(resultjson.result[i].goods.isInventory==1){
       						 html += ' <div class="price">￥'+resultjson.result[i].goods.price+'元  (库存  '+resultjson.result[i].goods.remain+')</div>';
       					 }else{
       						 html += ' <div class="price">￥'+resultjson.result[i].goods.price+'元 </div>';
       					 }
       					html += ' <div class="originalPrice">￥'+resultjson.result[i].goods.originalPrice+'元</div>';
       					 html += ' </a> </li>';
        					
        					
        				}	
        				$("#goodsShow").append(html);
        				
        				if(resultjson.result.length==0){
        					//$("#tip").text("暂无商品");
        				}else if(resultjson.result.length<20){
            				$("#loading").hide();
			  	            $("#tip").hide();
        				}else if(resultjson.result.length>=20){
 	  		            	$("#loading").hide();
 			  	            $("#tip").show();
 			  	            canLoad=true;
 	  		            }else{
 	  		            	$("#loading").hide();
 	  		            	/*$("#tip").text("我是有底线的");*/
 	  		            }
	        				  /* 评分 */
	  		   			score($(".scoreCon"));
	  		   		    myScroll.refresh();
	  		            main.max = myScroll.maxScrollY;
	  		            
	  		            console.log(main.max);

	  		           
		  	            
        			}
        		}
        	})                     
        }
    }

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
    	
        myScroll = new IScroll(".detail", {probeType: 3, mouseWheel: true, click: true, preventDefault: false});
        main.max = myScroll.maxScrollY;
        myScroll.on("scroll", move);
        
    }
})(jQuery);

main.init();
